package com.yulinlin.admin.modules.generate.domain;


import com.yulinlin.common.model.AbstractQueryModel;
import com.yulinlin.common.model.ModelSelectWrapper;
import com.yulinlin.data.core.anno.JoinField;
import com.yulinlin.data.core.anno.JoinTable;
import com.yulinlin.data.core.anno.JoinWhere;
import com.yulinlin.data.core.util.StringUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.List;
import java.util.Map;

@Data
@JoinTable("information_schema.columns t")
public class ColumnInfo extends AbstractQueryModel<ColumnInfo> {


    @ApiModelProperty("列明")
    @JoinField
    private String columnName;

    @ApiModelProperty("列类型")
    @JoinField
    private String columnType;

    @ApiModelProperty("列描述")
    @JoinField
    private String columnComment;

    @ApiModelProperty("数据库")
    @JoinWhere
    @JoinField
    private String tableSchema;

    @ApiModelProperty("表明")
    @JoinField
    @JoinWhere
    private String tableName;

    //获得数据库所有字段
    public static Map<String,List<ColumnInfo>> findByAll(String tableSchema){
        return ModelSelectWrapper.newInstance(ColumnInfo.class)
                .eq("tableSchema",tableSchema)
                .selectByGroup("tableName");
    }

    public String getFieldName(){
        return StringUtil.columnToJava(columnName);
    }


    public String getFieldType(){
        if(columnType.startsWith("varchar")){
            return "String";
        }else if(columnType.startsWith("date")){
            return  "Date";
        }else if(columnType.startsWith("tiny")){
            return  "Boolean";
        }else if(columnType.startsWith("int")){
            return  "Integer";
        }
        return  "String";
    }

}
